<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统模块" prop="title">
        <el-input
          v-model="queryForm.title"
          placeholder="请输入系统模块"
          clearable
          style="width: 200px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryForm.operName"
          placeholder="请输入操作人员"
          clearable
          style="width: 200px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="businessType">
        <el-select
          v-model="queryForm.businessType"
          placeholder="操作类型"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryForm.status"
          placeholder="操作状态"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-has-perm="['monitor_operLog:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-has-perm="['monitor_operLog:remove']"
        >清空
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :header-cell-style="thStyleFun" :cell-style="cellStyleFun"
              :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column label="日志编号" prop="operId"/>
      <el-table-column label="系统模块" prop="title" width="200" show-overflow-tooltip/>
      <el-table-column label="操作类型" prop="businessType" :formatter="typeFormat"/>
      <el-table-column label="请求方式" prop="requestMethod"/>
      <el-table-column label="操作人员" prop="operName"/>
      <el-table-column label="主机" prop="operIp" width="130" show-overflow-tooltip/>
      <el-table-column label="操作状态" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'danger' : 'success'">{{ statusFormat(scope.row) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作日期" prop="createTime" width="180"/>
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="detail"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-has-perm="['monitor_operLog:detail']"
          >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryForm.cursor"
      :limit.sync="queryForm.limit"
      @pagination="getPage"
    />

    <!-- 操作日志详细 -->
    <el-dialog v-dialog-drag title="操作日志详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.title }} / {{ typeFormat(form) }}</el-form-item>
            <el-form-item label="登录信息：">{{ form.operName }} / {{ form.operIp }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ form.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求体：">{{ form.operBody }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：" class="inputMaxRoll">
              {{ form.jsonResult }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运行时间：">{{ form.runTime + 'ms' }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作时间：">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" class="inputMaxRoll"
                          v-if="form.status === 1">{{ form.errorMsg }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {cleanOperLog, delOperLog, getOperLog, pageOperLog} from "@/api/monitor/operLog";
import {cellStyleFun, thStyleFun} from "@/utils/common";

export default {
  name: "OperLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [
        {value: 0, label: '其它'},
        {value: 1, label: '新增'},
        {value: 2, label: '查询'},
        {value: 3, label: '修改'},
        {value: 4, label: '删除'},
        {value: 5, label: '授权'},
        {value: 6, label: '导出'},
        {value: 7, label: '导入'},
        {value: 8, label: '强退'},
        {value: 9, label: '生成代码'},
        {value: 10, label: '清空数据'},
      ],
      // 类型
      statusOptions: [
        {value: 0, label: '正常'},
        {value: 1, label: '异常'}
      ],
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {
        operId: undefined,
        title: undefined,
        businessType: undefined,
        method: undefined,
        requestMethod: undefined,
        operType: undefined,
        operName: undefined,
        operUrl: undefined,
        operIp: undefined,
        operParam: undefined,
        operBody: undefined,
        jsonResult: undefined,
        runTime: undefined,
        status: undefined,
        errorMsg: undefined,
        createTime: undefined,
      },
      // 查询参数
      queryForm: {
        cursor: 1,
        limit: 10,
        title: undefined,
        operName: undefined,
        businessType: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    /** 查询登录日志 */
    getPage() {
      this.loading = true;
      pageOperLog(this.addDateRange(this.queryForm, this.dateRange, 1)).then(({result: res}) => {
          this.list = res.records;
          this.total = res.total;
          this.loading = false;
        }
      );
    },
    // 操作日志状态字典翻译
    statusFormat(row, column) {
      return this.matchingLabel(this.statusOptions, row.status)
    },
    // 操作日志类型字典翻译
    typeFormat(row, column) {
      return this.matchingLabel(this.typeOptions, row.businessType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.cursor = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId)
      this.multiple = !selection.length
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.reset();
      getOperLog(row.operId).then(({result: res}) => {
        this.form = res
        this.open = true;
      });
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids;
      this.$confirm('是否确认删除日志编号为 " ' + operIds + ' " 的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delOperLog(operIds);
      }).then(() => {
        this.getPage();
        this.$message.success("删除成功");
      })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm('是否确认清空所有操作日志数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return cleanOperLog();
      }).then(() => {
        this.getPage();
        this.$message.success("清空成功");
      })
    },
    // 表格居中 (html中使用需要先声明)
    thStyleFun,
    // 表格居中 (html中使用需要先声明)
    cellStyleFun
  }
};
</script>
<style scoped lang="scss">
.inputMaxRoll {
  max-height: 200px;
  overflow: hidden scroll;
  padding-right: 10px
}
</style>

